﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows;
using BrainTechLLC;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using ImageAppDataModels;
using BrainTechLLC.WPF;
using System.Dynamic;
using Expression = System.Linq.Expressions.Expression;

namespace ImageAppViewModels
{
    public partial class SettingsViewModel : GenericViewModelBase<SettingsViewModel, Settings>, INotifyPropertyChanged
    {
        public SettingsViewModel(Settings settings)
        {
            if (settings == null)
            {
                settings = new Settings();
            }

            Item = settings;
        }

        public void SaveSettings(string path)
        {
            Item.SaveSettings(path);
        }

        public List<InternalRenderingMethod> AllInternalRenderingMethods
        {
            get
            {
                List<InternalRenderingMethod> results = new List<InternalRenderingMethod>();

                var vals = Enum.GetValues(typeof(InternalRenderingMethod)).Cast<InternalRenderingMethod>();

                return vals.ToList();
            }
        }

        public bool ThumbnailDebugMode
        {
            get { return DebugSettings.DebugSettingsInstance.ShowDebug; }
            set
            {
                if (DebugSettings.DebugSettingsInstance.ShowDebug != value)
                {
                    DebugSettings.DebugSettingsInstance.ShowDebug = value;
                    OnPropertyChanged("ThumbnailDebugMode");
                }
            }
        }

        public static Settings LoadSettings(string path)
        {
            return Settings.LoadSettings(path);
        }

        public List<InterpolationMode> AllInterpolations
        {
            get
            {
                List<InterpolationMode> results = new List<InterpolationMode>();
                results.Add(InterpolationMode.Default);
                results.Add(InterpolationMode.Bilinear);
                results.Add(InterpolationMode.Bicubic);
                results.Add(InterpolationMode.NearestNeighbor);
                results.Add(InterpolationMode.Low);
                results.Add(InterpolationMode.High);
                results.Add(InterpolationMode.HighQualityBilinear);
                results.Add(InterpolationMode.HighQualityBicubic);
                return results;
            }
        }

        public List<PixelFormat> AllStandardColorPixelFormats
        {
            get
            {
                List<PixelFormat> results = new List<PixelFormat>();
                results.Add(PixelFormat.Format24bppRgb);
                results.Add(PixelFormat.Format32bppArgb);
                results.Add(PixelFormat.Format32bppPArgb);
                results.Add(PixelFormat.Format32bppRgb);
                results.Add(PixelFormat.Format48bppRgb);
                results.Add(PixelFormat.Format64bppArgb);
                results.Add(PixelFormat.Format64bppPArgb);
                results.Add(PixelFormat.Format16bppRgb555);
                results.Add(PixelFormat.Format16bppRgb565);
                results.Add(PixelFormat.Format16bppArgb1555);
                return results;
            }
        }
        public List<SmoothingMode> AllSmoothingModes
        {
            get
            {
                List<SmoothingMode> results = new List<SmoothingMode>();
                results.Add(SmoothingMode.None);
                results.Add(SmoothingMode.Default);
                results.Add(SmoothingMode.AntiAlias);
                results.Add(SmoothingMode.HighQuality);
                results.Add(SmoothingMode.HighSpeed);
                return results;
            }
        }

        public List<string> AllSortByOptions
        {
            get
            {
                List<string> results = new List<string>();

                results.Add(string.Empty);
                results.Add("FileNameOnly");
                results.Add("FileName");
                results.Add("FileSize");
                results.Add("FileDateTime");
                results.Add("WidthHeight");

                return results;
            }
        }

        public void ApplySettings(Window mainWindow)
        {
            Settings.Values.BlurCutoff = Item.BlurCutoff;

            if (Item.MaxBitmapsInMemory < 4)
                Item.MaxBitmapsInMemory = 4;

            if (Item.ItemsToTossWhenMaxReached > Item.MaxBitmapsInMemory || Item.ItemsToTossWhenMaxReached < 1)
                Item.ItemsToTossWhenMaxReached = 1;

            if (Item.MaxImageSourcesInMemory < 4)
                Item.MaxImageSourcesInMemory = 4;

            if (Item.ImageSourcesToTossWhenMaxReached > Item.MaxImageSourcesInMemory || Item.ImageSourcesToTossWhenMaxReached < 1)
                Item.ImageSourcesToTossWhenMaxReached = 1;

            if (Item.MaxFileBytesInMemory < 4)
                Item.MaxFileBytesInMemory = 4;

            if (Item.FileBytesToTossWhenMaxReached > Item.MaxFileBytesInMemory || Item.FileBytesToTossWhenMaxReached < 1)
                Item.FileBytesToTossWhenMaxReached = 1;

            RecentlyUsedFileImages.RecentlyUsedBitmaps.MaxItems = Item.MaxBitmapsInMemory;
            RecentlyUsedFileImages.RecentlyUsedBitmaps.ItemsToTossWhenMaxReached = Item.ItemsToTossWhenMaxReached;
            RecentlyUsedFileImages.RecentlyUsedImageSources.MaxItems = Item.MaxImageSourcesInMemory;
            RecentlyUsedFileImages.RecentlyUsedImageSources.ItemsToTossWhenMaxReached = Item.ImageSourcesToTossWhenMaxReached;
            RecentlyUsedFileImages.RecentlyUsedFileBytes.MaxItems = Item.MaxFileBytesInMemory;
            RecentlyUsedFileImages.RecentlyUsedFileBytes.ItemsToTossWhenMaxReached = Item.FileBytesToTossWhenMaxReached;

            if (mainWindow.WindowState != Item.MainWindowState)
            {
                mainWindow.WindowState = Item.MainWindowState;
            }

            if (Item.Window1Size != null)
            {
                if (Item.MainWindowState == WindowState.Normal)
                {
                    if (Item.Window1Size.Width == 0)
                    {
                        mainWindow.WindowState = WindowState.Maximized;
                    }
                    else
                    {
                        mainWindow.Left = Item.Window1Size.Left;
                        mainWindow.Top = Item.Window1Size.Top;
                        mainWindow.Width = Item.Window1Size.Width;
                        mainWindow.Height = Item.Window1Size.Height;
                    }
                }
            }

            
        }

        public void ReadSettings(Window mainWindow)
        {
            if (mainWindow.WindowState == WindowState.Normal)
            {
                Item.Window1Size = new Rectangle((int)mainWindow.Left, (int)mainWindow.Top, (int)mainWindow.ActualWidth, (int)mainWindow.ActualHeight);
            }

            Item.MainWindowState = mainWindow.WindowState;
        }

        public void SetThumbWidthAndHeight(double w, double h)
        {
            Item.SetThumbWidthAndHeight(w, h);
        }

        public bool SortDescending
        {
            get { return Item.SortDescending; }
            set { Item.SortDescending = value; }
        }

        public string SortBy
        {
            get { return Item.SortBy; }
            set { Item.SortBy = value; }
        }
    }

}
